package com.zss.dao.impl;

import com.zss.bean.Product;
import com.zss.dao.ProductDao;
import com.zss.util.DruidUtil;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;

public class ProductDaoImpl implements ProductDao {
    @Override
    public ArrayList<Product> selectList() {
        Connection con = DruidUtil.getConnection();
        String sql = "select * from product";
        DruidUtil.SetResultBean query = null;
        ArrayList<Product> list = new ArrayList<>();
        try {
            query = DruidUtil.query(con, sql, null);
            while (query != null && query.getRs().next()) {
                int id = query.getRs().getInt("id");
                String productName = query.getRs().getString("productName");
                double price = query.getRs().getDouble("price");
                String description = query.getRs().getString("description");
                list.add(new Product(id, productName, price, description));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            DruidUtil.close(con, query);
        }

        return list;
    }

    @Override
    public boolean addProduct(String productName, double price, String description) {
        Connection con = DruidUtil.getConnection();
        String sql = "insert product (productName,price,description) values(?,?,?)";
        Object[] params = {productName, price, description};
        DruidUtil.IntResultBean update = null;
        try {
            update = DruidUtil.update(con, sql, params);
            return update != null && update.getResult() > 0;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            DruidUtil.close(con, update);
        }
        return false;
    }

    @Override
    public boolean delProduct(long id) {
        boolean flag = false;
        if (id == -1) {
            throw new IllegalArgumentException();
        }
        Connection con = DruidUtil.getConnection();
        String sql = "delete from product where id=?";
        Object[] params = {id};
        DruidUtil.IntResultBean update = null;
        try {
            update = DruidUtil.update(con, sql, params);
            return update != null && update.getResult() > 0;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            DruidUtil.close(con, update);
        }
        return false;
    }

    @Override
    public boolean updProduct(Product product) {
        boolean flag = false;
        if (product == null) {
            throw new IllegalArgumentException();
        }
        if (product.getId() == -1) {
            throw new NullPointerException("The product's id. can not be null");
        }

        Connection con = DruidUtil.getConnection();
        String sql = "update product set productName=?,price=?,description=? where id=?";
        String description = product.getDescription();
        double price = product.getPrice();
        String productName = product.getProductName();
        long id = product.getId();
        Object[] params = {productName, price, description,id};
        DruidUtil.IntResultBean update = null;
        try {
            update = DruidUtil.update(con, sql, params);
            return update != null && update.getResult() > 0;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            DruidUtil.close(con, update);
        }
        return false;
    }

}
